package com.tedu.jsd2407_ivos_fzj.geofence.service.impl;

import com.tedu.jsd2407_ivos_fzj.base.exception.ServiceException;
import com.tedu.jsd2407_ivos_fzj.base.response.StatusCode;
import com.tedu.jsd2407_ivos_fzj.geofence.mapper.GeofenceMapper;
import com.tedu.jsd2407_ivos_fzj.geofence.pojo.dto.GeofenceParam;
import com.tedu.jsd2407_ivos_fzj.geofence.pojo.dto.GeofenceQuery;
import com.tedu.jsd2407_ivos_fzj.geofence.pojo.entity.Geofence;
import com.tedu.jsd2407_ivos_fzj.geofence.pojo.entity.GeofenceVehicle;
import com.tedu.jsd2407_ivos_fzj.geofence.pojo.vo.GeofenceVO;
import com.tedu.jsd2407_ivos_fzj.geofence.service.GeofenceService;
import com.tedu.jsd2407_ivos_fzj.vehicle.mapper.VehicleMapper;
import com.tedu.jsd2407_ivos_fzj.vehicle.pojo.dto.VehicleQuery;
import com.tedu.jsd2407_ivos_fzj.vehicle.pojo.entity.Vehicle;
import com.tedu.jsd2407_ivos_fzj.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.incrementer.AbstractColumnMaxValueIncrementer;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public void save(GeofenceParam geofenceParam) {
        log.debug("围栏保存业务，参数{}",geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        if(geofenceParam.getId()==null){
            //添加围栏业务
            geofence.setCreateTime(new Date());
            geofence.setStatus("1");
            geofenceMapper.insert(geofence);
        }else{
            //修改围栏业务
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }
    }

    @Override
    public List<GeofenceVO> select(GeofenceQuery geofenceQuery) {
        log.debug("围栏查询业务，参数：{}",geofenceQuery);
        List<GeofenceVO> geofenceVOList = geofenceMapper.select(geofenceQuery);
        log.debug("围栏查询业务，围栏结果集1：{}",geofenceVOList);
        for(GeofenceVO geofenceVO :geofenceVOList){
            assignGeofenceVO(geofenceVO);
        }
        return geofenceVOList;
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏业务，参数：{}",id);
        //判断当前围栏上是否有已绑定的车辆，有的话不删除
            //VehicleQuery
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> vehicleVOList = vehicleMapper.select(vehicleQuery);
        log.debug("当前围栏已绑定车辆集合，参数：{}",vehicleVOList);
        if(vehicleVOList!=null&&!vehicleVOList.isEmpty()){//集合需要判断非null，非空
            //不删除，抛出异常（先解绑车辆才可删除围栏）
            throw new ServiceException(StatusCode.OPERATION_FAILED);

            /*解绑删除围栏绑定的车辆*//*
            GeofenceVehicle geofenceVehicle = new GeofenceVehicle();
            geofenceVehicle.setGeofenceId(id);
            geofenceVehicle.setUpdateTime(new Date());
            geofenceVehicle.setGeofenceBindStatus("0");
            geofenceMapper.removeGeofenceBindStatus(geofenceVehicle);
            *//*删除围栏*//*
            geofenceMapper.deleteById(id);*/
        }else{
            /*删除围栏*/
            geofenceMapper.deleteById(id);
        }
    }
    public void assignGeofenceVO(GeofenceVO geofenceVO){
        int totalNum=0;
        int availableNum=0;
        List<Vehicle> vehicleList=new ArrayList<>();
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(geofenceVO.getId());
        List<VehicleVO> vehicleVOList = vehicleMapper.select(vehicleQuery);
        for(VehicleVO vehicleVO :vehicleVOList){
            totalNum=totalNum+1;
            if(vehicleVO.getStatus().equals("1")){
                availableNum= availableNum+1;
                Vehicle vehicle = new Vehicle();
                BeanUtils.copyProperties(vehicleVO,vehicle);
                vehicleList.add(vehicle);
            }
        }
        geofenceVO.setTotalNum(totalNum);
        geofenceVO.setAvailableNum(availableNum);
        geofenceVO.setVehicleList(vehicleList);
    }
}
